import openai
from flask import Flask,request,Response
from flask_cors import CORS

app = Flask(__name__)

# 解决跨域问题
CORS(app,supports_credentials=True)

@app.route("/")

def home():

    return "Hello!"

# V0版本：用户自己提供organization、api_key
@app.route("/showTextV0",methods=['POST'])

def showTextV0():
    openai.organization=request.form.get('organization')
    openai.api_key = request.form.get('api_key')
    textParam = request.form.get('comments')
    textLog = openai.organization , openai.api_key , textParam
    print(textLog,flush=True)
    textResponse = openai.Completion.create(
        model="text-davinci-003",
        prompt=textParam,
        max_tokens=1024,
        temperature=0.2
    )
    textResult = textResponse.choices[0].text
    print(textResult,flush=True)
    return textResult

@app.route("/showImageV0",methods=['POST'])

def showImageV0():
    openai.organization=request.form.get('organization')
    openai.api_key = request.form.get('api_key')
    imgParam = request.form.get('comments')
    imgLog = openai.organization , openai.api_key , imgParam
    print(imgLog,flush=True)
    imgResponse = openai.Image.create(
        prompt=imgParam,
        n=1,
        size="256x256"
    )
    ImgResult = imgResponse['data'][0]['url']
    print(ImgResult,flush=True)
    return ImgResult


# V1版本：用户无需提供organization、api_key
@app.route("/showTextV1",methods=['POST'])

def showTextV1():
    // 注释掉了
    openai.organization = ""
    openai.api_key = ""
    textParam = request.form.get('comments')
    textLog = openai.organization , openai.api_key , textParam
    print(textLog,flush=True)
    textResponse = openai.Completion.create(
        model="text-davinci-003",
        prompt=textParam,
        max_tokens=1024,
        temperature=0.5
    )
    textResult = textResponse.choices[0].text
    print(textResult,flush=True)
    return textResult

@app.route("/showImageV1",methods=['POST'])

def showImageV1():
    // 注释掉了
    openai.organization = ""
    openai.api_key = ""
    imgParam = request.form.get('comments')
    imgLog = openai.organization + ',' + openai.api_key + ',' + imgParam
    print(imgLog,flush=True)
    imgResponse = openai.Image.create(
        prompt=imgParam,
        n=1,
        size="256x256"
    )
    ImgResult = imgResponse['data'][0]['url']
    print(ImgResult,flush=True)
    return ImgResult

# V2版本：改为流式编程，用户体验更加丝滑
@app.route("/showTextV2",methods=['POST'])
def showTextV2():
    // 注释掉了
    openai.organization = ""
    openai.api_key = ""
    textParam = request.form.get('comments')
    textLog = openai.organization, openai.api_key, textParam
    print(textLog, flush=True)
    textResponse = openai.Completion.create(
        model="text-davinci-003",
        prompt=textParam,
        max_tokens=1024,
        temperature=0.5,
        stream=True,
        stop=None
    )

    # 返回流式响应
    def generate():
        try:
            while True:
                chunk = next(textResponse)
                yield chunk.choices[0].text
        except StopIteration:
            pass

    response_headers = {
        'Content-Type': 'text/plain',
        'Transfer-Encoding': 'chunked'
    }

    return Response(generate(), headers=response_headers)

@app.route("/showImageV2",methods=['POST'])

def showImageV2():
    // 注释掉了
    openai.organization = ""
    openai.api_key = ""
    imgParam = request.form.get('comments')
    imgLog = openai.organization , openai.api_key , imgParam
    print(imgLog,flush=True)
    imgResponse = openai.Image.create(
        prompt=imgParam,
        n=1,
        size="256x256"
    )
    ImgResult = imgResponse['data'][0]['url']
    print(ImgResult,flush=True)
    return ImgResult
